package com.bmw.seed.service.impl;

import com.bmw.seed.dao.ROrderDao;
import com.bmw.seed.model.ROrder;
import com.bmw.seed.service.ROrderService;
import com.bmw.seed.util.bean.OrderReq;
import com.bmw.seed.util.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Classname: ROrderServiceImpl
 * Package: com.bmw.seed.service.impl
 * Description:
 *
 * @Author 王少伟
 * @Create 2024/1/31 10:39
 * @Version 1.8
 */
@Service("rOrderService")
public class ROrderServiceImpl implements ROrderService {
    @Autowired
    ROrderDao rOrderDao;
    @Autowired
    RedisUtil redisUtil;
    @Override
    public Integer order(OrderReq orderReq) {
        //保存数据库
        ROrder rOrder = new ROrder();
        rOrder.setUserId(orderReq.getUserId());
        rOrder.setOrderAmount(orderReq.getAmount());
        rOrder.setIsDeleted(0);
        rOrder.setCreateTime(new Date());
        int i = rOrderDao.insert(rOrder);
        if(i > 0){
            //更新缓存中的排行榜数据
            String key = "rank:list";
            redisUtil.zincrby(key,String.valueOf(orderReq.getUserId()),orderReq.getAmount().doubleValue());
        }
        return i;
    }
}
